package com.cqwshzj.backgroundjob.config;/**
 * @Description:
 * @Auther: Huang Zhijin
 * @Version: 1.0.0
 * @Date: 2021/4/8 18:48
 */

import com.cqwshzj.backgroundjob.job.ScanJobInfoAndAddToZkDaemonJob;
import com.cqwshzj.backgroundjob.job.ScanJobStrategyAndDynamicAddJob;
import com.cqwshzj.framework.manager.ElasticJobManager;
import com.cqwshzj.util.ElasticJobUtil;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 * 〈〉
 *
 * @author Huang Zhijin
 * @create 2021/4/8
 * @since 1.0.0
 */
@Configuration
public class BackgroundJobConfig {

	private static final Logger log = LoggerFactory.getLogger(BackgroundJobConfig.class);

	@Autowired
	private ZookeeperRegistryCenter registryCenter;

	@Autowired
	ElasticJobManager jobManager;

	@Bean
	public ScanJobInfoAndAddToZkDaemonJob scanJobInfoAndAddToZkDaemonJob(){
		return new ScanJobInfoAndAddToZkDaemonJob();
	}

	@Bean
	@ConditionalOnProperty("jobinfo.cron")
	public JobScheduler scanJobInfoAndAddToZkDaemonJobScheduler(
			ScanJobInfoAndAddToZkDaemonJob simpleJob,
			@Value("${jobinfo.cron}") String cron,
			@Value("${jobinfo.shardingTotalCount}") int shardingTotalCount,
			@Value("${jobinfo.shardingItemParameters}") String shardingItemParameters){

		log.debug(" parameter ready. jobinfo.cron=[{}] , jobinfo.shardingTotalCount=[{}] , jobinfo.shardingItemParameters=[{}] .",cron,shardingTotalCount,shardingItemParameters);

//		String jobName = simpleJob.getClass().getSimpleName()+ RandomUtils.nextInt(10,100)+"_"+ JobConstant.DEVICE_IP;
		String jobName = simpleJob.getClass().getSimpleName()+ RandomUtils.nextInt(10,100);

		final SpringJobScheduler springJobScheduler = ElasticJobUtil.createSpringJobScheduler(jobName, simpleJob, cron, shardingTotalCount, shardingItemParameters,registryCenter);
		springJobScheduler.init();
		return springJobScheduler;
	}

	@Bean
	public ScanJobStrategyAndDynamicAddJob scanJobStrategyAndDynamicAddJob(){
		return new ScanJobStrategyAndDynamicAddJob();
	}


	@Bean
	@ConditionalOnProperty("jobstrategy.cron")
	public JobScheduler scanJobStrategyAndDynamicAddJobScheduler(
			ScanJobStrategyAndDynamicAddJob simpleJob,
			@Value("${jobstrategy.cron}") String cron,
			@Value("${jobstrategy.shardingTotalCount}") int shardingTotalCount,
			@Value("${jobstrategy.shardingItemParameters}") String shardingItemParameters){

		log.debug(" parameter ready. jobstrategy.cron=[{}] , jobstrategy.shardingTotalCount=[{}] , jobstrategy.shardingItemParameters=[{}] .",cron,shardingTotalCount,shardingItemParameters);

//		String jobName = simpleJob.getClass().getSimpleName()+ RandomUtils.nextInt(10,100)+"_"+ JobConstant.DEVICE_IP;
		String jobName = simpleJob.getClass().getSimpleName()+ RandomUtils.nextInt(10,100);

		final SpringJobScheduler springJobScheduler = ElasticJobUtil.createSpringJobScheduler(jobName, simpleJob, cron, shardingTotalCount, shardingItemParameters, registryCenter);
		springJobScheduler.init();
		return springJobScheduler;
	}



}
